package com.egu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.egu.common.util.DateUtil;
import com.egu.common.util.UserUtil;
import com.egu.mapper.CustomerMapper;
import com.egu.mapper.CustomerVoMapper;
import com.egu.model.Customer;
import com.egu.model.CustomerExample;
import com.egu.model.CustomerExample.Criteria;
import com.egu.model.User;
import com.egu.service.CustomerService;
import com.egu.vo.CustomerVo;
import com.egu.vo.PageParam;
import com.egu.vo.RequestPageVo;
import com.egu.vo.RequestResultVo;
import com.egu.vo.VoForCustomer;

@Service
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	CustomerMapper customerMapper;
	@Autowired
	CustomerVoMapper customerVoMapper;

	/*public RequestPageVo<Customer> getList(PageParam param, Customer customer) {
		RequestPageVo<Customer> result = new RequestPageVo<Customer>();
		CustomerExample example = new CustomerExample();
		CustomerExample.Criteria createCriteria = example.createCriteria();
		int count = (int) customerMapper.countByExample(example);
		if (count > 0) {
			example.setStartPage(param.getOffset());
			example.setPageSize(param.getLimit());
			List<Customer> list = customerMapper.selectByExample(example);
			result.setRows(list);
			result.setTotal(count);
		}
		return result;
	}*/

	public RequestResultVo<List<CustomerVo>> getAllList() {
		RequestResultVo<List<CustomerVo>> result = new RequestResultVo<List<CustomerVo>>();

		CustomerExample example = new CustomerExample();
		CustomerExample.Criteria createCriteria = example.createCriteria();

		createCriteria.andIsUsedEqualTo("1");
		List<Customer> list = customerMapper.selectByExample(example);

		List<CustomerVo> customerlist = this.voListForCustomer(list);
		result.setResult(customerlist);
		return result;
	}

	public RequestPageVo<CustomerVo> getVoList(PageParam param, CustomerVo customerVo) {
		RequestPageVo<CustomerVo> result = new RequestPageVo<CustomerVo>();
		Customer customer = new Customer();
		BeanUtils.copyProperties(customerVo, customer);

		CustomerExample example = new CustomerExample();
		CustomerExample.Criteria createCriteria = example.createCriteria();

		createCriteria.andIsUsedEqualTo("1");
		// userId不能为空 为空表示尚未分配 不应该出现在客户列表
		createCriteria.andUserIdIsNotNull();
		User currentUser = UserUtil.getCurrentUser();
		if (currentUser.getId() != 1) {
			createCriteria.andUserIdEqualTo(currentUser.getId());
		}
		if (customer.getCustomerName() != null) {
			createCriteria.andCustomerNameLike("%" + customer.getCustomerName() + "%");
		}
		int count = (int) customerMapper.countByExample(example);
		if (count > 0) {
			example.setStartPage(param.getOffset());
			example.setPageSize(param.getLimit());
			List<Customer> list = customerMapper.selectByExample(example);

			List<CustomerVo> customerlist = this.voListForCustomer(list);
			result.setRows(customerlist);
			result.setTotal(count);
		}
		return result;
	}

	public List<CustomerVo> voListForCustomer(List<Customer> list) {
		ArrayList<CustomerVo> customerVoList = new ArrayList<>();
		for (Customer customer : list) {
			CustomerVo customerVo = new CustomerVo();
			BeanUtils.copyProperties(customer, customerVo);
			List<VoForCustomer> vo = new ArrayList<>();
			if (customerVo.getUserId() == null) {
				vo = customerVoMapper.getVoForCustomerDistrVoById(customerVo.getId());
			} else {
				vo = customerVoMapper.getVoForCustomerVoById(customerVo.getId());
			}
			if (vo.size() > 0) {
				VoForCustomer voForCustomer = vo.get(0);
				customerVo.setConditionName(voForCustomer.getConditionName());
				customerVo.setSourceName(voForCustomer.getSourceName());
				customerVo.setTypeName(voForCustomer.getTypeName());
				customerVo.setUserName(voForCustomer.getUserName());
			}
			customerVoList.add(customerVo);
		}
		return customerVoList;
	}

	@Override
	@Transactional
	public RequestResultVo<Customer> saveOrUpdate(Customer customer) {
		User currentUser = UserUtil.getCurrentUser();
		if (customer.getId() == null) {
			customer.setCustomerAddman(currentUser.getUserName());
			customer.setCustomerAddtime(DateUtil.getCurrentDate());
			customerMapper.insertSelective(customer);
		} else {
			customer.setChangeMan(currentUser.getUserName());
			customer.setCustomerChangtime(DateUtil.getCurrentDate());
			customerMapper.updateByPrimaryKeySelective(customer);
		}
		return RequestResultVo.success();
	}

	@Override
	public RequestResultVo<Customer> deleteDataById(Integer id) {
		User currentUser = UserUtil.getCurrentUser();
		Customer customer = new Customer();
		customer.setIsUsed("0");
		customer.setId(id);
		customer.setChangeMan(currentUser.getUserName());
		customer.setCustomerChangtime(DateUtil.getCurrentDate());
		try {
			customerMapper.updateByPrimaryKeySelective(customer);
		} catch (Exception e) {
			return RequestResultVo.error("数据库删除失败");
		}
		return RequestResultVo.success();
	}

	@Override
	public RequestPageVo<CustomerVo> getVoList(PageParam param) {
		RequestPageVo<CustomerVo> result = new RequestPageVo<CustomerVo>();

		CustomerExample example = new CustomerExample();
		CustomerExample.Criteria createCriteria = example.createCriteria();

		createCriteria.andIsUsedEqualTo("1");
		createCriteria.andUserIdIsNull();
		/*
		 * User currentUser = UserUtil.getCurrentUser(); if (currentUser.getId() != 1) {
		 * createCriteria.andUserIdEqualTo(currentUser.getId()); }
		 */

		int count = (int) customerMapper.countByExample(example);
		if (count > 0) {
			example.setStartPage(param.getOffset());
			example.setPageSize(param.getLimit());
			List<Customer> list = customerMapper.selectByExample(example);

			List<CustomerVo> customerlist = this.voListForCustomer(list);
			result.setRows(customerlist);
			result.setTotal(count);
		}
		return result;
	}

	// 获取当前用户的 客户
	@Override
	public RequestResultVo<List<Customer>> getDataByCurrentUser() {
		RequestResultVo<List<Customer>> requestResultVo = new RequestResultVo<List<Customer>>();
		User currentUser = UserUtil.getCurrentUser();
		CustomerExample example = new CustomerExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsUsedEqualTo("1");
		//if (currentUser.getRolePower()==1) {
			criteria.andUserIdEqualTo(currentUser.getId());
		//}
		List<Customer> list = customerMapper.selectByExample(example);
		requestResultVo.setResult(list);
		return requestResultVo;
	}

	// 用于 发送邮件获取 客户邮箱
	@Override
	public Customer getCustomertById(Integer customerId) {
		if (customerId == null) {
			return null;
		}
		
		CustomerExample example = new CustomerExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(customerId);
		criteria.andIsUsedEqualTo("1");
		List<Customer> list = customerMapper.selectByExample(example);
		if(list.size()>0) {
			return list.get(0);
		}
		return null;
	}

}
